<template>
  <div class="did-h">
    <div class="black black-h">
      <div>批量创建账号界面（仅管理员有）</div>
      <el-row :gutter="20" class="mt3">
        <el-col :span="10" :offset="6">
        </el-col>
      </el-row>
      <el-form ref="ruleForm" :model="form" :rules="rules">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" label="序号" align="center"></el-table-column>
          <el-table-column prop="name" label="用户名" align="center">
            <template slot-scope="scope">
              <el-input v-model="scope.row.name" placeholder="请输入姓名"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="number" label="士兵号" align="center">
            <template slot-scope="scope">
              <el-input v-model="scope.row.number" placeholder="请输入士兵号"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="department" label="部门" align="center">
            <template slot-scope="scope">
              <el-input v-model="scope.row.department" placeholder="请输入部门"></el-input>
            </template>
          </el-table-column>
          <el-table-column align="center" width="50">
            <template slot-scope="scope">
              <i v-if="tableData.length > 1" class="el-icon-remove-outline cursor" @click="delClick(scope.$index)"></i>
            </template>
          </el-table-column>
        </el-table>
        <div class="add-button" @click="addClick" >添加</div>
        <div class="did-button">
          <el-button size="medium" type="primary" @click="publication">创建账号</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import NoData from '../../../src/components/noData.vue';
export default {
  components: {
    NoData,
  },
  data() {
    return {
      activeName: 'first',
      tableData: [{ name: '', number: '', department: '' }],
      form: { 
        account: {},
      },
      rules: { // 表单验证规则
        // 根据需要定义验证规则
        name: [
          { required: true, message: '请输入姓名/装备', trigger: 'blur' }
        ],
        number: [
          { required: true, message: '请输入士兵号/装备号', trigger: 'blur' }
        ],
        department: [
          { required: true, message: '请输入部门', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    publication() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.BatchCreation();
        } else {
          return false;
        }
      });
    },
    addClick() {
      let obj = { name: '', number: '', department: '' };
      this.tableData.push(obj);
    },
    delClick(index) {
      this.tableData.splice(index, 1);
    },
    async BatchCreation() {
      this.form.account = {};
      this.tableData.forEach(item => {
        this.form.account[item.name] = {
          name:item.name,
          number: item.number,
          department: item.department
        };
      });
      console.log(this.form)
      try {
        let res = await this.$http.post(this.$api.userIdentity.BatchCreation, this.form);
        if (res.code == 200) {
          let att = [];
          this.data = res.data;
          let arr = JSON.parse(res.data.claim);
          for (const resKey in arr) {
            att.push({
              name: resKey,
              value: arr[resKey]
            });
          }
          console.log(att);
          this.claim = att;
        }
      } catch (error) {
        // 处理错误
      }
    },
  },
};
</script>

<style lang="less" scoped>
.did-content {
  margin-top: 10%;
  h1 {
    color: #666666;
    width: 320px;
    margin: 0 auto;
  }
}
.did-button {
  width: 100px;
  margin: 0 auto;
  margin-top: 20px;
}
.m-a0 {
  margin: 0 auto;
}
.black-h {
  height: 50%;
  overflow-y: auto;
}
.black-height {
  height: calc(50% - 90px);
  overflow-y: auto;
}
.black-height ul{
  width: 90%;
  margin: 0 auto;
}
.nodata {
  margin-top: 10%;
}
.did-width {
  width: 500px;
  margin: 0 auto;
}

.add-button {
  width: 100%;
  padding: 8px 0;
  background: #fca400;
  color: white;
  border: 1px dashed #ccc;
  text-align: center;
  border-radius: 3px;
  cursor: pointer;
}
</style>
